﻿namespace FlexiCommerce.Web.UI.DynamicData
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Xml;

    public class FlexiTableProvider : System.Web.DynamicData.ModelProviders.TableProvider
    {
        #region Fields

        private ReadOnlyCollection<System.Web.DynamicData.ModelProviders.ColumnProvider> columns;
        private XmlNode node;

        #endregion Fields

        #region Constructors

        public FlexiTableProvider(FlexiModelProvider model,XmlNode node)
            : base(model)
        {
            if (node == null)
            throw new ArgumentNullException("node");
            this.node = node;

            this.EntityType = System.Type.GetType(node.Attributes["name"].Value);
            this.Name = EntityType.Name;
            IList<System.Web.DynamicData.ModelProviders.ColumnProvider> temp = new List<System.Web.DynamicData.ModelProviders.ColumnProvider>();
            foreach (XmlNode property in node.ChildNodes)
            {
            if (property.Name == "property" || property.Name == "id")
            {
                temp.Add((System.Web.DynamicData.ModelProviders.ColumnProvider)(new FlexiColumnProvider(this, property)));
            }
            }

            columns = new ReadOnlyCollection<System.Web.DynamicData.ModelProviders.ColumnProvider>(temp);
        }

        #endregion Constructors

        #region Properties

        public override System.Collections.ObjectModel.ReadOnlyCollection<System.Web.DynamicData.ModelProviders.ColumnProvider> Columns
        {
            get { return columns; ; }
        }

        #endregion Properties

        #region Methods

        public override IQueryable GetQuery(object context)
        {
            throw new NotImplementedException();
        }

        #endregion Methods
    }
}